package work.lrl.coupon_send;

import cn.hutool.core.util.StrUtil;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.read.listener.ReadListener;
import io.netty.channel.nio.NioEventLoopGroup;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;

import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.atomic.AtomicInteger;

@Slf4j
public class Setup3 {
    public static void main(String[] args) {
        EasyExcel.read(Setting.resultFile, Demo.ExcelObject.class, new Setup3Lis()).sheet().doRead();
    }

    public static class Setup3Lis implements ReadListener<Demo.ExcelObject> {
        List<Demo.ExcelObject> list = new CopyOnWriteArrayList<>();
        AtomicInteger count = new AtomicInteger();
        NioEventLoopGroup nioEventLoopGroup = new NioEventLoopGroup(32);


        @Override
        public void invoke(Demo.ExcelObject excelObject, AnalysisContext analysisContext) {
            nioEventLoopGroup.execute(() -> {
                if (count.getAndIncrement() % 2000 == 0) {
                    log.info("【卡券群发】搜集系统异常用户 进度 ->{}", count.get());
                }
                if ("否".equals(excelObject.getHit()) ||
                        "是".equals(excelObject.getSuccess()) ||
                        (StrUtil.isNotBlank(excelObject.getErrMsg()) &&
                                excelObject.getErrMsg().contains("对不起，您还不是会员，请先注册") )
                ) {
                    return;
                }
                list.add(excelObject);
            });
        }


        @SneakyThrows
        @Override
        public void doAfterAllAnalysed(AnalysisContext analysisContext) {
            nioEventLoopGroup.shutdownGracefully().sync();
            EasyExcel.write(Setting.setup3, Demo.ExcelObject.class).sheet("sheet1").doWrite(list);

        }
    }
}
